package baiwa.rta.listener;

import java.sql.Driver;
import java.sql.DriverManager;
import java.util.Enumeration;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;

public class ApplicationListener implements ServletContextListener {
	private static final Logger logger = Logger.getLogger(ApplicationListener.class);
	private ServletContext context = null;

	public void contextInitialized(ServletContextEvent event) {
		logger.info("Context Initialized");
		this.context = event.getServletContext();
	}

	public void contextDestroyed(ServletContextEvent event) {
		logger.info("Context Destroyed");
		String prefix = getClass().getSimpleName() + " destroy() ";
		try {
			Enumeration<Driver> drivers = DriverManager.getDrivers();
			while (drivers.hasMoreElements()) {
				DriverManager.deregisterDriver(drivers.nextElement());
			}
		} catch (Exception e) {
			context.log("\n" + prefix + "Exception caught while deregistering JDBC drivers", e);
		}
		context.log("\n" + prefix + "complete");
	}

}